package cn.iocoder.yudao.module.patent.controller.admin.authors;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.patent.controller.admin.authors.vo.*;
import cn.iocoder.yudao.module.patent.dal.dataobject.authors.AuthorsDO;
import cn.iocoder.yudao.module.digital.dal.dataobject.patent.PatentDO;
import cn.iocoder.yudao.module.patent.service.authors.AuthorsService;

@Tag(name = "管理后台 - 专利作者信息")
@RestController
@RequestMapping("/patent/authors")
@Validated
public class AuthorsController {

    @Resource
    private AuthorsService authorsService;

    @PostMapping("/create")
    @Operation(summary = "创建专利作者信息")
    @PreAuthorize("@ss.hasPermission('patent:authors:create')")
    public CommonResult<Integer> createAuthors(@Valid @RequestBody AuthorsSaveReqVO createReqVO) {
        return success(authorsService.createAuthors(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专利作者信息")
    @PreAuthorize("@ss.hasPermission('patent:authors:update')")
    public CommonResult<Boolean> updateAuthors(@Valid @RequestBody AuthorsSaveReqVO updateReqVO) {
        authorsService.updateAuthors(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专利作者信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('patent:authors:delete')")
    public CommonResult<Boolean> deleteAuthors(@RequestParam("id") Integer id) {
        authorsService.deleteAuthors(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专利作者信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('patent:authors:query')")
    public CommonResult<AuthorsRespVO> getAuthors(@RequestParam("id") Integer id) {
        AuthorsDO authors = authorsService.getAuthors(id);
        return success(BeanUtils.toBean(authors, AuthorsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专利作者信息分页")
    @PreAuthorize("@ss.hasPermission('patent:authors:query')")
    public CommonResult<PageResult<AuthorsRespVO>> getAuthorsPage(@Valid AuthorsPageReqVO pageReqVO) {
        PageResult<AuthorsDO> pageResult = authorsService.getAuthorsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuthorsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专利作者信息 Excel")
    @PreAuthorize("@ss.hasPermission('patent:authors:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuthorsExcel(@Valid AuthorsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuthorsDO> list = authorsService.getAuthorsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专利作者信息.xls", "数据", AuthorsRespVO.class,
                        BeanUtils.toBean(list, AuthorsRespVO.class));
    }

    // ==================== 子表（专利信息） ====================

    @GetMapping("/patent/page")
    @Operation(summary = "获得专利信息分页")
    @Parameter(name = "id", description = "专利主键ID")
    @PreAuthorize("@ss.hasPermission('patent:authors:query')")
    public CommonResult<PageResult<PatentDO>> getPatentPage(PageParam pageReqVO,
                                                                                        @RequestParam("id") Integer id) {
        return success(authorsService.getPatentPage(pageReqVO, id));
    }

    @PostMapping("/patent/create")
    @Operation(summary = "创建专利信息")
    @PreAuthorize("@ss.hasPermission('patent:authors:create')")
    public CommonResult<Integer> createPatent(@Valid @RequestBody PatentDO patent) {
        return success(authorsService.createPatent(patent));
    }

    @PutMapping("/patent/update")
    @Operation(summary = "更新专利信息")
    @PreAuthorize("@ss.hasPermission('patent:authors:update')")
    public CommonResult<Boolean> updatePatent(@Valid @RequestBody PatentDO patent) {
        authorsService.updatePatent(patent);
        return success(true);
    }

    @DeleteMapping("/patent/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除专利信息")
    @PreAuthorize("@ss.hasPermission('patent:authors:delete')")
    public CommonResult<Boolean> deletePatent(@RequestParam("id") Integer id) {
        authorsService.deletePatent(id);
        return success(true);
    }

	@GetMapping("/patent/get")
	@Operation(summary = "获得专利信息")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('patent:authors:query')")
	public CommonResult<PatentDO> getPatent(@RequestParam("id") Integer id) {
	    return success(authorsService.getPatent(id));
	}

}